import {Component, ViewChild} from '@angular/core';
import {
  NavParams,
  Events,
  AlertController,
  App,
  ModalController,
  NavController,
  Content,
  ActionSheetController,
  LoadingController
} from 'ionic-angular';
import {Storage} from '@ionic/storage';
import {Config} from '../../../../providers/config';
import {DomSanitizer} from '@angular/platform-browser';
import _ from 'lodash';
import * as moment from "moment";

import {Common} from "../../../../providers/common";
import {IpmCirculateModal} from "../../../ipm/circulate/circulate.modal";
import {IrpPositionListService} from "./list.service";

@Component({
  selector: 'page-irp-position-list',
  templateUrl: './list.html',
  providers: [IrpPositionListService]
})
export class IrpPositionListPage {
  moa_page_name: any = "page-irp-position-list";
  moa_page_explain: any = "持仓管理";

  positionType = "zhzj";

  showQueryCondition = true;

  isLoadMore = false;

  selectedDate = "";

  selectedFund = [];

  positionDataList = [];

  fundAssetsData = null;

  loginid = "";

  constructor(public alertCtrl: AlertController,
              public app: App,
              public modalCtrl: ModalController,
              public navCtrl: NavController,
              public config: Config,
              private sanitizer: DomSanitizer,
              public events: Events,
              public storage: Storage,
              private common: Common,
              private actionSheetCtrl: ActionSheetController,
              private navParams: NavParams,
              public loadingCtrl: LoadingController,
              public listService: IrpPositionListService) {
    this.selectedDate = moment().format("YYYY-MM-DD");
    this.storage.get("userInfo").then(data => {
      this.loginid = data.loginid;
      console.log(data)
    })
  }

  setShowQueryCondition() {
    this.showQueryCondition = !this.showQueryCondition;
  }

  queryData() {
    this.isLoadMore = true;
    this.showQueryCondition = false;
      this.listService.getO32Code(this.loginid).subscribe((data) => {
        let fundCode = this.selectedFund.length ? this.selectedFund[0].vc_fund_code : '';
        if (data) {
          let l_operator_no = data[0].l_operator_no;
          console.log(data)
          this.listService.getPositionDataList(l_operator_no, fundCode).subscribe(data => {
            this.positionDataList = [];
            if (data) {
              console.log(data);
              this.positionDataList = data;
            }
          });
          this.listService.getFundAssetsData(l_operator_no, fundCode).subscribe(data => {
            this.fundAssetsData = null;
            if (data) {
              console.log(data);
              this.fundAssetsData = data[0];
            }
          })
        }
      });

  }

  selectFund() {
    let profileModal = this.modalCtrl.create(IpmCirculateModal, {
      selectedFund: this.selectedFund,
      fundType: "1",
      selectOne: true
    });
    profileModal.onDidDismiss(data => {
      if (data && data != "close") {
        console.log(data);
        this.selectedFund = [];
        this.selectedFund.push(data);
      }
    });
    profileModal.present();
  }

}
